/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.ide.commons.search;

import com.rw.magallanes.core.Item;
import com.rw.magallanes.core.Warehouse;
import com.rw.magallanes.ide.main.MainFrame;
import com.rw.magallanes.ide.main.MainSystem;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Gonzalez
 */
public class SearchEngine {

    public static List<Item> search(String keyword) {
        MainSystem ms = MainSystem.getInstance();
        MainFrame mf = ms.getMainFrame();
        Warehouse wh = mf.getWarehouse();
        List<Item> allItems = wh.getAllItems();
        if (keyword != null && !keyword.isEmpty()) {
            Pattern p = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE);

            List<Item> items = new ArrayList<Item>(2);
            for (Item item : allItems) {
                String name = item.getName();
                Matcher matcher = p.matcher(name);
                if (matcher.find()) {
                    items.add(item);
                }
            }
            return items;
        } else {
            return allItems;
        }
    }
}
